import { useRef, useEffect, useState } from 'react'

const useTable = () => {
  const pageRef = useRef(null)
  const headerRef = useRef(null)
  const [loading, setLoading] = useState(false)
  const initPages = {
    pageSize: 10,
    current: 1
  }
  const pages = useRef(initPages)
  const [total, setTotal] = useState(0)
  const [height, setHeight] = useState(500)
  const getHeight = () => { // 获取表格高度
    if (!pageRef.current) return
    const height = pageRef.current.offsetHeight - headerRef.current.offsetHeight - 170
    setHeight(height)
  }
  useEffect(() => { // 监听窗口大小变化
    getHeight()
    window.addEventListener('resize', getHeight)
    return () => window.removeEventListener('resize', getHeight)
  }, [])

  return {
    headerRef,
    pageRef,
    tableScroll: {
      x: 1000,
      y: height
    },
    pages,
    initPages,
    setTotal,
    total,
    setLoading,
    loading
  }
}

export default useTable